﻿using Inovout.Cloud.Data.Blob.Models;
using Inovout.Cloud.Data.Blob.Repositories;
using Inovout.Runtime;
using Inovout.Services;
using Microsoft.Hadoop.WebHDFS;
using NHibernate.Criterion;
using System;
using System.Threading;

namespace Inovout.Cloud.Data.Blob.Service
{
    public class DirectoryService : RepositoryService<Directory>, IDirectoryService
    {
        public bool Exists(string path)
        {
            return base.Repository.Exists(Restrictions.Eq("Path", path).IgnoreCase());
        }
        public Directory FindByPath(string path)
        {
            return base.Repository.Find(Restrictions.Eq("Path", path).IgnoreCase());
        }
        //public Directory Create(string path)
        //{
        //    string[] directoryNames = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        //    string directoryPath = "/";
        //    Directory directory = null;
        //    Directory parentDirectory = null;
        //    foreach (string directoryName in directoryNames)
        //    {
        //        directoryPath += directoryName + "/";
        //        directory = base.Repository.Find(Restrictions.Eq("Path", directoryPath));
        //        if (directory == null)
        //        {
        //            directory = new Directory
        //            {
        //                Name = directoryName,
        //                Owner = Thread.CurrentPrincipal.Identity.Name,
        //                Path = directoryPath,
        //                Parent = parentDirectory
        //            };
        //            base.Save(directory);
        //        }
        //        parentDirectory = directory;
        //    }
        //    return directory;
        //}

        public Directory Create(string path)
        {
            string[] directoryNames = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            string directoryPath = "";
            Directory directory = null;
            Directory parentDirectory = null;
            foreach (string directoryName in directoryNames)
            {
                directoryPath += directoryName + "\\";
                directory = base.Repository.Find(Restrictions.Eq("Path", directoryPath));
                if (directory == null)
                {
                    directory = new Directory
                    {
                        Name = directoryName,
                        Owner = Thread.CurrentPrincipal.Identity.Name,
                        Path = directoryPath,
                        Parent = parentDirectory
                    };
                    base.Save(directory);
                }
                parentDirectory = directory;
            }
            return directory;
        }


        public long GetUsedSpace(string owner)
        {
            //暂时调用FILE表来做SUM。
            FileRepository fileRepository = ComponentRegistry.Resolve<FileRepository>();
            return fileRepository.SumSpace(owner);
        }

        private WebHDFSClient GetWebHDFSClient(Directory directory)
        {
            IApplication application = ComponentRegistry.Resolve<IApplication>();
            return new WebHDFSClient(new Uri(application.Settings["HdfsUri"].ToString()), directory.Owner);
        }

        public void Delete(Directory directory)
        {
            var hdfsClient = GetWebHDFSClient(directory);
            hdfsClient.DeleteDirectory(directory.Path);
            base.Delete((object)directory.Id);
        }
    }
}
